// pages/profile/index.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar: "",
    nickName: ""
  },
  onLoad() {
    this.getUserInfo()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  async getUserInfo() {
    let res = await wx.http.get('/userInfo')
    console.log(res)
    let { avatar, nickName } = res
    if (!!avatar) {
      this.setData({
        avatar
      })
    }
    if (!!nickName) {
      this.setData({
        nickName
      })
    }
  },

  async chooseAvatar(e) {
    console.log(e.detail.avatarUrl)
    let res = await wx.http.upload('/upload', {
      name: 'file', // 文件的 key
      filePath: e.detail.avatarUrl, // 文件的 value
      formData: {
        type: "avatar"
      }, // // HTTP 请求中其他额外的 form data
    })
    this.setData({
      avatar: res.url
    })
  },

  chooseNickname(e) {
    let name = e.detail.value
    if (!name) {
      return wx.showToast({
        title: '昵称不能为空',
        icon: 'none'
      })
    }

    wx.showModal({
      title: '提示',
      content: '确定修改昵称为：' + name + '吗？',
      success: (res) => {
        if (res.confirm) {
          this.changeNickname(name)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  async changeNickname(name) {
    let res = await wx.http.put('/userInfo', { nickName: name })
    console.log(res)
    this.setData({
      nickName: name
    })
  }



})